﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MSIACCSL2.Web.BtbElseServices;
using WcfService1.DTO;

namespace MSIACCSL2.Web.Posting
{
    public partial class PostingBTBElse : System.Web.UI.Page
    {
        private BtbElseServicesClient btbClient;
        public PostingBTBElse()
        {
            btbClient = new BtbElseServicesClient();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
           ShowData();
        }

        void ShowData()
        {
            if (Page.IsValid)
            {
                var btbList = btbClient.GetListByPeriode(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")),
                                                    DateTime.Parse(txtTgl2.Text, new CultureInfo("ID-id")));
                GridView1.DataSource = btbList.OrderBy(x => x.NoBtb);
                GridView1.DataBind();
            }
        
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                Label lblStatus = (Label)row.FindControl("lblStatus");
                if (lblStatus.Text == "0")
                    lblStatus.Text = "Open";
                else if (lblStatus.Text == "1")
                    lblStatus.Text = "Close";
                else if (lblStatus.Text == "2")
                    lblStatus.Text = "Void";
                else if (lblStatus.Text == "3")
                    lblStatus.Text = "Post";
            }
        }

        protected void btnCekAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chk1 = (CheckBox) row.FindControl("chk1");
                chk1.Checked = true;
            }
        }

        protected void btnPosting_Click(object sender, EventArgs e)
        {
            var btbs = new List<BtbElseDTO>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox check = (CheckBox) row.FindControl("chk1");
                    if (check.Checked)
                    {
                        btbs.Add(new BtbElseDTO(){NoBtb = row.Cells[0].Text});
                    }
                }
            }

            btbClient.Posting(btbs.ToArray());
            ShowData();
            Literal1.Text = "Proses Posting Selesai";
        }

        protected void btnUnPosting_Click(object sender, EventArgs e)
        {
            var btbs = new List<BtbElseDTO>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox check = (CheckBox)row.FindControl("chk1");
                    if (check.Checked)
                    {
                        btbs.Add(new BtbElseDTO() { NoBtb = row.Cells[0].Text });
                    }
                }
            }

            btbClient.UnPosting(btbs.ToArray());
            ShowData();
            Literal1.Text = "Proses UnPosting Selesai";
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
              DateTime result;
            args.IsValid = DateTime.TryParse(txtTgl1.Text, out result) && DateTime.TryParse(txtTgl2.Text, out result);
        }
    }
}